﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using BLL;
using Model;

public partial class PersonInfo_OrderList : BasePage
{
    TheOrderBO tbo = new TheOrderBO ();
    PersonInfoBO pbo = new PersonInfoBO();
    GiftBO gbo = new GiftBO();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (GetQeuryString("para") == "GetList")
        {
            BindData();
        }
        else if (GetQeuryString("para") == "delete")
        {
            try
            {
                Response.Clear();
                string id = GetQeuryString("id");
                TheOrder to = tbo.GetModel(int.Parse(id));
                to.States = 2;
                tbo.Update(to);
                try
                {
                    PersonInfo p = pbo.GetModel(int.Parse(to.PID.ToString()));
                    Gift g=gbo.GetModel(int.Parse(to.GID.ToString()));
                    new SendDX().Send("您兑换的商品["+g.Name+"]已发货,请注意查收。",p.Phone,0);
                }
                catch (Exception)
                {
                    
                }
                Response.Write("ok");
            }
            catch (Exception ex)
            {
                Response.Write("Error" + ex.Message);
            }
            finally
            {
                Response.End();
            }

        }
        else if (GetQeuryString("para") == "delete1")
        {
            try
            {
                Response.Clear();
                string id = GetQeuryString("id");
                TheOrder to = tbo.GetModel(int.Parse(id));
                to.States = 3;
                tbo.Update(to);
                Response.Write("ok");
            }
            catch (Exception ex)
            {
                Response.Write("Error" + ex.Message);
            }
            finally
            {
                Response.End();
            }

        }
        else if (GetQeuryString("para") == "delete2")
        {
            try
            {
                Response.Clear();
                string id = GetQeuryString("id");
                TheOrder to = tbo.GetModel(int.Parse(id));
                to.States = 4;
                tbo.Update(to);
                Response.Write("ok");
            }
            catch (Exception ex)
            {
                Response.Write("Error" + ex.Message);
            }
            finally
            {
                Response.End();
            }

        }

    }

    public string BM(string oldvalue)
    {
        return Server.UrlEncode(oldvalue);
    }

    private void BindData()
    {
        Response.Clear();
        //string s = "{\"total\":\"239\",\"rows\":[  {\"code\":\"001\",\"name\":\"Name 1\",\"addr\":\"Address 11\",\"addr\":\"col4 data\"}   ]}  ";
        int PageNumber = Convert.ToInt32(Request.Params["page"] == null ? "1" : Request.Params["page"]) - 1;
        int pageSize = Convert.ToInt32(Request.Params["rows"] == null ? "10" : Request.Params["rows"]);
        string sort = " order by " + (Request.Params["sort"] == null ? "id" : Request.Params["sort"]) + " " + (Request.Params["order"] == null ? " desc " : Request.Params["order"]);
        string condition = " 1=1 ";
        //string uid = txtUID.Text;// Request.Params["txtUID"] == null ? "" : Request.Params["txtUID"];
        //string username = txtUserName.Text;// Request.Params["UserName"] == null ? "" : Request.Params["UserName"];
        txtTime1.Text = Request.Params["Time1"] == null ? "" : Request.Params["Time1"];
        txtTime2.Text = Request.Params["Time2"] == null ? "" : Request.Params["Time2"];
        txtJF.Text = Request.Params["JF"] == null ? "" : Request.Params["JF"];
        DropDownList1.SelectedValue = Request.Params["text"] == null ? "" : Request.Params["text"];
        txtDZ.Text = Request.Params["dz"] == null ? "" : Server.UrlDecode(Request.Params["dz"]);
        txtDY.Text = Request.Params["dy"] == null ? "" : Request.Params["dy"];
        txtJT.Text = Request.Params["jt"] == null ? "" : Request.Params["jt"];
        txtMD.Text = Request.Params["md"] == null ? "" : Request.Params["md"];
        if (txtTime1.Text != "")
            condition += " AND t.CreateDate>='"+txtTime1.Text+"' ";
        if (txtTime2.Text != "")
            condition += " AND t.CreateDate<='" + DateTime.Parse(txtTime2.Text).AddDays(1) + "' ";
        if (txtJF.Text != "")
            condition += "  AND g.Integral="+txtJF.Text+" ";
        if (DropDownList1.SelectedValue != "0")
            condition += " AND t.States="+DropDownList1.SelectedValue+" ";
        if (txtDY.Text != "")
            condition += " and p.Name='"+txtDY.Text+"' ";
        if (txtDZ.Text != "")
            condition += " and (p1.Name='"+txtDZ.Text+"' OR p.Name='"+txtDZ.Text+"') ";
        if (txtJT.Text != "")
            condition += "  AND p.JT='" + txtJT.Text + " '";
        if (txtMD.Text != "")
            condition += " AND p.Store='" + txtMD.Text + "' ";
        DataSet ds = access.GetAllOrder(sort, pageSize, PageNumber, condition);
        string re = ToJson.DataTableToJson(ds.Tables[0].Rows[0][0].ToString(), "rows", ds.Tables[1]);
        Response.Write(re);
        Response.End();
    }
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        BindData();
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        //string s = "{\"total\":\"239\",\"rows\":[  {\"code\":\"001\",\"name\":\"Name 1\",\"addr\":\"Address 11\",\"addr\":\"col4 data\"}   ]}  ";
        int PageNumber = Convert.ToInt32(Request.Params["page"] == null ? "1" : Request.Params["page"]) - 1;
        int pageSize = Convert.ToInt32(Request.Params["rows"] == null ? "10" : Request.Params["rows"]);
        string sort = " order by " + (Request.Params["sort"] == null ? "id" : Request.Params["sort"]) + " " + (Request.Params["order"] == null ? " desc " : Request.Params["order"]);
        string condition = " 1=1 ";
        //string uid = txtUID.Text;// Request.Params["txtUID"] == null ? "" : Request.Params["txtUID"];
        //string username = txtUserName.Text;// Request.Params["UserName"] == null ? "" : Request.Params["UserName"];
        if (txtTime1.Text != "")
            condition += " AND t.CreateDate>='" + txtTime1.Text + "' ";
        if (txtTime2.Text != "")
            condition += " AND t.CreateDate<='" + DateTime.Parse(txtTime2.Text).AddDays(1) + "' ";
        if (txtJF.Text != "")
            condition += "  AND g.Integral=" + txtJF.Text + " ";
        if (DropDownList1.SelectedValue != "0")
            condition += " AND t.States=" + DropDownList1.SelectedValue + " ";
        if (txtDY.Text != "")
            condition += " and p.Name='" + txtDY.Text + "' ";
        if (txtDZ.Text != "")
            condition += " and (p1.Name='" + txtDZ.Text + "' OR p.Name='" + txtDZ.Text + "') ";
        if (txtJT.Text != "")
            condition += "  AND p.JT='" + txtJT.Text + " '";
        if (txtMD.Text != "")
            condition += " AND p.Store='" + txtMD.Text + "' ";
        DataSet ds = access.GetAllOrderExcel(sort, pageSize, PageNumber, condition);
        CreateExcel(ds, "订单");
    }
}